﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace BusinessLogic
{
    public class DonDatHangKH
    {
        public Entities.DonDatHangKH[] SelectDonDatHangKH()
        {
            SqlConnection con = DataAccess.Connection.OpenConnection();
            DataTable dt = new DataTable();
            Entities.DonDatHangKH[] ddhkh = null;
            try
            {
                 
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "sp_DonHangKH_Select";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = con;

                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();

                adapter.Fill(ds, "ddhkh");
                dt = ds.Tables["ddhkh"];

                var temp = from ddh in dt.AsEnumerable()
                           select new Entities.DonDatHangKH
                           {
                               DonDatHangKHID = ddh.Field<int>("DonDatHangKHID"),
                               MaDonDatHangKH = ddh.Field<string>("MaDonDatHangKH"),
                               NgayDonHang = ddh.Field<DateTime>("NgayDonHang"),
                               MaKhachHang = ddh.Field<string>("MaKhachHang"),
                               TenKhachHang = ddh.Field<string>("TenKhachHang"),
                               NoHienThoi = ddh.Field<string>("NoHienThoi"),
                               TrangThaiDonDatHang = ddh.Field<string>("TrangThaiDonDatHang"),
                               NgayNhapDuKien = ddh.Field<DateTime>("NgayNhapDuKien"),
                               HinhThucThanhToan = ddh.Field<string>("HinhThucThanhToan"),
                               MaKho = ddh.Field<string>("MaKho"),
                               TenKho = ddh.Field<string>("TenKho"),
                               MaNhanVien = ddh.Field<string>("MaNhanVien"),
                               TenNhanVien = ddh.Field<string>("TenNhanVien"),
                               MaTienTe = ddh.Field<string>("MaTienTe"),
                               GhiChu = ddh.Field<string>("GhiChu"),
                               Deleted = ddh.Field<bool>("Deleted")
                           };
                ddhkh = temp.ToArray<Entities.DonDatHangKH>();
            }
            catch
            {
                ddhkh = new Entities.DonDatHangKH[0];
            }
            finally
            {
                con.Close();
            }
            return ddhkh;
        }
        public int InsertDonDatHangKH(Entities.DonDatHangKH ddhkh)
        {
            SqlConnection con = DataAccess.Connection.OpenConnection();
            DataTable dt = new DataTable();
            int i = 0;
            try
            {
                 
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "sp_DonHangKH_Insert";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = con;

                cmd.Parameters.Add("@MaDonDatHangKH", SqlDbType.VarChar, 50).Value = ddhkh.MaDonDatHangKH;
                cmd.Parameters.Add("@NgayDonHang", SqlDbType.DateTime).Value = ddhkh.NgayDonHang;
                cmd.Parameters.Add("@MaKhachHang", SqlDbType.VarChar, 50).Value = ddhkh.MaKhachHang;
                cmd.Parameters.Add("@TenKhachHang", SqlDbType.NVarChar, 200).Value = ddhkh.TenKhachHang;
                cmd.Parameters.Add("@NoHienThoi", SqlDbType.Float).Value = ddhkh.NoHienThoi;
                cmd.Parameters.Add("@TrangThaiDonDatHang", SqlDbType.NVarChar, 20).Value = ddhkh.TrangThaiDonDatHang;
                cmd.Parameters.Add("@NgayNhapDuKien", SqlDbType.DateTime).Value = ddhkh.NgayNhapDuKien;
                cmd.Parameters.Add("@HinhThucThanhToan", SqlDbType.NVarChar, 20).Value = ddhkh.HinhThucThanhToan;
                cmd.Parameters.Add("@MaKho", SqlDbType.VarChar, 50).Value = ddhkh.MaKho;
                cmd.Parameters.Add("@TenKho", SqlDbType.NVarChar, 200).Value = ddhkh.TenKho;
                cmd.Parameters.Add("@MaNhanVien", SqlDbType.VarChar, 50).Value = ddhkh.MaNhanVien;
                cmd.Parameters.Add("@TenNhanVien", SqlDbType.NVarChar, 200).Value = ddhkh.TenNhanVien;
                cmd.Parameters.Add("@MaTienTe", SqlDbType.VarChar, 50).Value = ddhkh.MaTienTe;
                cmd.Parameters.Add("@GhiChu", SqlDbType.VarChar, 50).Value = ddhkh.GhiChu;
                cmd.Parameters.Add("@Deleted", SqlDbType.Bit).Value = ddhkh.Deleted;

                i = cmd.ExecuteNonQuery();
                LuuVietHeThong.LuuViet("Insert", ddhkh.TaiKhoan.MaNhanVien, "Thêm đơn đặt hàng: " + ddhkh.MaDonDatHangKH, ddhkh.TaiKhoan.TenDangNhap, DateTime.Now.ToString("hh:mm dd/MM/yyyy"));
            }
            catch
            {
                i = -2;
            }
            finally
            {
                con.Close();
            }
            return i;
        }
        public int DeleteDonDatHangKH(Entities.DonDatHangKH ddhkh)
        {
            SqlConnection con = DataAccess.Connection.OpenConnection();
            DataTable dt = new DataTable();
            int i = 0;
            try
            {
                 
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "sp_DonHangKH_Delete";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = con;

                cmd.Parameters.Add("@MaDonDatHangKH", SqlDbType.VarChar, 50).Value = ddhkh.MaDonDatHangKH;
                

                i = cmd.ExecuteNonQuery();
                LuuVietHeThong.LuuViet("Delete", ddhkh.TaiKhoan.MaNhanVien, "Xóa đơn đặt hàng: " + ddhkh.MaDonDatHangKH, ddhkh.TaiKhoan.TenDangNhap, DateTime.Now.ToString("hh:mm dd/MM/yyyy"));
            }
            catch
            {
                i = -2;
            }
            finally
            {
                con.Close();
            }
            return i;
        }
    }
}
